package com.springboot.chapter4.aspect;

import com.springboot.chapter4.aspect.pojo.User;
import com.springboot.chapter4.validator.UserValidator;
import com.springboot.chapter4.validator.impl.UserValidatorImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 作者：paopao
 * 版本：1.0
 * 定义切面、切点
 */
@Aspect
public class MyAspect {
    @Pointcut("execution(* com.springboot.chapter4.aspect.service.impl.UserServiceImpl.printUser(..))")
    public void pointCut() {}

    @Before("pointCut()")
    public void before() {
        System.out.println("before............");
    }

    @Before("pointCut() && args(user)")
    public void beforeParam(JoinPoint point, User user) {
        Object[] args = point.getArgs();
        System.out.println("beforeParam............" + user);
    }

    @After("pointCut()")
    public void after() {
        System.out.println("after............");
    }

    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("afterReturning............");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("afterThrowing............");
    }

    @Around("pointCut()")
    public void around(ProceedingJoinPoint jp) {
        System.out.println("around before............");
        try {
            // 回调目标对象的原有方法
            jp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("around after............");
    }

    @DeclareParents(
            value = "com.springboot.chapter4.aspect.service.impl.UserServiceImpl+",
            defaultImpl = UserValidatorImpl.class
    )
    public UserValidator userValidator;
}
